package com.seuic.hnjiningposapp.hnapps.ui.view.dialog;

import android.content.Context;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.core.BasePopupView;
import com.seuic.hnjiningposapp.R;
import com.seuic.hnjiningposapp.hnapps.base.BaseActivity;
import com.seuic.hnjiningposapp.hnapps.ui.activity.ConstantrotationActivity;
import com.seuic.hnjiningposapp.hnapps.util.GetMoneyLvUtils;
import com.seuic.hnjiningposapp.hnapps.util.HttpUtilsV2;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 选择活转定时间及返回相关信息
 */
public class SelHtoDSaveTimePp extends BasePopupView implements CompoundButton.OnCheckedChangeListener {
    @BindView(R.id.select_of_maturity_3mbtn)
    RadioButton selectOfMaturity3mbtn;
    @BindView(R.id.select_of_maturity_6mbtn)
    RadioButton selectOfMaturity6mbtn;
    @BindView(R.id.select_of_maturity_1ybtn)
    RadioButton selectOfMaturity1ybtn;
    @BindView(R.id.select_of_maturity_2ybtn)
    RadioButton selectOfMaturity2ybtn;
    @BindView(R.id.select_of_maturity_3ybtn)
    RadioButton selectOfMaturity3ybtn;
    @BindView(R.id.select_of_maturity_5ybtn)
    RadioButton selectOfMaturity5ybtn;
    @BindView(R.id.rate_of_interest)
    TextView rateOfInterest;
    @BindView(R.id.interest)
    TextView interest;
    @BindView(R.id.total)
    TextView total;
    @BindView(R.id.automatic_renewal)
    CheckBox automaticRenewal;
    @BindView(R.id.bx_rb)
    RadioButton bxRb;
    @BindView(R.id.bj_rb)
    RadioButton bjRb;
    @BindView(R.id.rg)
    RadioGroup rg;
    @BindView(R.id.unloading_money_ed)
    EditText unloadingMoneyEd;
    @BindView(R.id.next_btn)
    Button nextBtn;

    BaseActivity mBaseActivity;
    CallBackHtoDinfo mCallBackHtoDinfo;

    String sel_time = "三个月";
    Unbinder bind;
    @BindView(R.id.user_bal)
    TextView userBal;

    String UserBal;

    String ProductCode = "815TD011002";
    String name = "三个月整存整取存款";
    int m = 3;
    @BindView(R.id.query_ll)
    Button queryLl;
    //利率
    private String interests = "";
    //利息
    private Double lx;

    public SelHtoDSaveTimePp(@NonNull Context context, BaseActivity baseActivity, CallBackHtoDinfo callBackHtoDinfo) {
        super(context);
        this.mBaseActivity = baseActivity;
        this.mCallBackHtoDinfo = callBackHtoDinfo;
    }

    @Override
    protected int getPopupLayoutId() {
        return R.layout.selection_of_maturity_layout;
    }

    public void setUserBal(String userBal) {
        UserBal = userBal;
    }

    @Override
    protected void onCreate() {
        bind = ButterKnife.bind(this);
        super.onCreate();

        selectOfMaturity3mbtn.setOnClickListener(this::onViewClicked);
        selectOfMaturity6mbtn.setOnClickListener(this::onViewClicked);
        selectOfMaturity1ybtn.setOnClickListener(this::onViewClicked);
        selectOfMaturity2ybtn.setOnClickListener(this::onViewClicked);
        selectOfMaturity3ybtn.setOnClickListener(this::onViewClicked);
        selectOfMaturity5ybtn.setOnClickListener(this::onViewClicked);
        selectOfMaturity3mbtn.setChecked(true);
        automaticRenewal.setOnCheckedChangeListener(this);


        userBal.setText("￥: " + GetMoneyLvUtils.getMoney(UserBal));
    }

    private void goSearch() {
        if (unloadingMoneyEd.getText().toString().equals("请输入转存金额")) {
            ToastUtils.showLong("请输入转存金额");
            return;
        }

        Double allm = Double.valueOf(GetMoneyLvUtils.getMoney(UserBal));
        Double mm; //本金
        DecimalFormat myformat = new DecimalFormat("#0.00");
        mm = Double.parseDouble(unloadingMoneyEd.getText().toString());//装换为double类型
        mm = Double.parseDouble(myformat.format(mm));//保留小数

        if (mm > allm) {
            ToastUtils.showLong("转存金额大于余额");
            return;
        }
        if (mm < 50) {
            ToastUtils.showLong("转存金额不能小于50");
            return;
        }
        Double finalMm = mm;
        HttpUtilsV2.getFixedTurn(ProductCode, unloadingMoneyEd.getText().toString(), new HttpUtilsV2.NetCallBack() {


            @Override
            public void onSuccess(String ServerSeqNo, String ConsumerSeqNo, JSONObject rspBody) {
                mBaseActivity.runOnUiThread(() -> {
                    interests = GetMoneyLvUtils.getLv(rspBody.getString("PD_INTR1"));
                    rateOfInterest.setText(GetMoneyLvUtils.getLv(rspBody.getString("PD_INTR1")));//利率
//                    年利率/12*期限*本金
                    String lv = rateOfInterest.getText().toString().replaceAll("%", "");
                    Double intlv = Double.valueOf(lv);
                    lx = Double.valueOf(myformat.format(intlv / 12 * m * finalMm / 100));
                    SelHtoDSaveTimePp.this.interest.setText(lx + "");//利息
                    total.setText(Double.valueOf(myformat.format(finalMm + lx)) + "");//本息合计
                });
            }

            @Override
            public void onError(String code, String msg) {
                ToastUtils.showLong(msg);
            }
        });


    }

    @OnClick({R.id.query_ll, R.id.next_btn, R.id.bx_rb, R.id.bj_rb, R.id.select_of_maturity_3mbtn, R.id.select_of_maturity_6mbtn, R.id.select_of_maturity_1ybtn, R.id.select_of_maturity_2ybtn, R.id.select_of_maturity_3ybtn, R.id.select_of_maturity_5ybtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.query_ll:
                goSearch();
                break;
            case R.id.bj_rb:
                //本金转存
                bjRb.setChecked(true);
                bxRb.setChecked(false);
                break;
            case R.id.bx_rb:
                //本息转存
                bjRb.setChecked(false);
                bxRb.setChecked(true);
                break;
            case R.id.select_of_maturity_3mbtn:
                //布局的原因不能自动实现rg的选中效果 ，手动实现以下
                sel_time = "三个月";
                m = 3;
                ProductCode = "815TD011002";
                name = "三个月整存整取存款";
                selectOfMaturity3mbtn.setChecked(true);
                selectOfMaturity6mbtn.setChecked(false);
                selectOfMaturity1ybtn.setChecked(false);
                selectOfMaturity2ybtn.setChecked(false);
                selectOfMaturity3ybtn.setChecked(false);
                selectOfMaturity5ybtn.setChecked(false);
                break;
            case R.id.select_of_maturity_6mbtn:
                sel_time = "六个月";
                m = 6;
                ProductCode = "815TD011003";
                name = "六个月整存整取存款";
                selectOfMaturity3mbtn.setChecked(false);
                selectOfMaturity6mbtn.setChecked(true);
                selectOfMaturity1ybtn.setChecked(false);
                selectOfMaturity2ybtn.setChecked(false);
                selectOfMaturity3ybtn.setChecked(false);
                selectOfMaturity5ybtn.setChecked(false);
                break;
            case R.id.select_of_maturity_1ybtn:
                sel_time = "一年";
                m = 12;
                ProductCode = "815TD011004";
                name = "一年整存整取存款";
                selectOfMaturity3mbtn.setChecked(false);
                selectOfMaturity6mbtn.setChecked(false);
                selectOfMaturity1ybtn.setChecked(true);
                selectOfMaturity2ybtn.setChecked(false);
                selectOfMaturity3ybtn.setChecked(false);
                selectOfMaturity5ybtn.setChecked(false);
                break;
            case R.id.select_of_maturity_2ybtn:
                sel_time = "两年";
                m = 24;
                ProductCode = "815TD011005";
                name = "两年整存整取存款";
                selectOfMaturity3mbtn.setChecked(false);
                selectOfMaturity6mbtn.setChecked(false);
                selectOfMaturity1ybtn.setChecked(false);
                selectOfMaturity2ybtn.setChecked(true);
                selectOfMaturity3ybtn.setChecked(false);
                selectOfMaturity5ybtn.setChecked(false);
                break;
            case R.id.select_of_maturity_3ybtn:
                sel_time = "三年";
                m = 36;
                ProductCode = "815TD011006";
                name = "三年整存整取存款";
                selectOfMaturity3mbtn.setChecked(false);
                selectOfMaturity6mbtn.setChecked(false);
                selectOfMaturity1ybtn.setChecked(false);
                selectOfMaturity2ybtn.setChecked(false);
                selectOfMaturity3ybtn.setChecked(true);
                selectOfMaturity5ybtn.setChecked(false);
                break;
            case R.id.select_of_maturity_5ybtn:
                sel_time = "五年";
                m = 60;
                ProductCode = "815TD011007";
                name = "五年整存整取存款";
                selectOfMaturity3mbtn.setChecked(false);
                selectOfMaturity6mbtn.setChecked(false);
                selectOfMaturity1ybtn.setChecked(false);
                selectOfMaturity2ybtn.setChecked(false);
                selectOfMaturity3ybtn.setChecked(false);
                selectOfMaturity5ybtn.setChecked(true);
                break;
            case R.id.next_btn:
                if (sel_time.equals("")) {
                    ToastUtils.showLong("请选择存期");
                    return;
                }
                if (unloadingMoneyEd.getText().toString().equals("")) {
                    ToastUtils.showLong("请输入转存金额");
                    return;
                }

                if (interests.equals("")) {
                    ToastUtils.showLong("请查询利率");
                    return;
                }
                if (automaticRenewal.isChecked()) {
                    //自动转存
                    if (bxRb.isChecked()) {
                        //本息
                        mCallBackHtoDinfo.HtDinfo(
                                ProductCode,
                                sel_time,
                                unloadingMoneyEd.getText().toString(),
                                interests,
                                lx + "",
                                total.getText().toString(),
                                name,
                                true,
                                ConstantrotationActivity.BX
                        );
                    } else if (bjRb.isChecked()) {
                        //本金
                        mCallBackHtoDinfo.HtDinfo(
                                ProductCode,
                                sel_time,
                                unloadingMoneyEd.getText().toString(),
                                interests,
                                lx + "",
                                total.getText().toString(),
                                name,
                                true,
                                ConstantrotationActivity.BJ);
                    }
                } else {
                    mCallBackHtoDinfo.HtDinfo(
                            ProductCode,
                            sel_time,
                            unloadingMoneyEd.getText().toString(),
                            interests,
                            lx + "",
                            total.getText().toString(),
                            name,
                            false,
                            "");
                }
                break;

        }
    }

    /**
     * time 存期
     * money 金额
     * rateOfInterest 利率
     * interest 利息
     * total 合计
     * unlading 自动转存
     * unloadingtype 自动转存类型
     */
    public interface CallBackHtoDinfo {
        void HtDinfo(String ProductCode, String time, String money, String rateOfInterest, String interest, String total, String name, boolean unlading, String unloadingtype);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.automatic_renewal:
                if (isChecked) {
                    rg.setVisibility(View.VISIBLE);
                } else {
                    rg.setVisibility(View.GONE);

                }
                break;

        }
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
        //注解解绑
        if (bind != null && bind != Unbinder.EMPTY) {
            bind.unbind();
        }
        this.bind = null;
    }

}
